package com.ywqf_api.controller;

import com.ywqf_api.common.enums.FilePaths;
import com.ywqf_api.dto.exc.ReturnExcution;
import com.ywqf_api.dto.in.RentForSaleDto;
import com.ywqf_api.entity.RentSale;
import com.ywqf_api.enums.ReturnEnum;
import com.ywqf_api.service.RentForSaleService;
import com.ywqf_api.util.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import java.util.Map;

/**
 * Created by Administrator on 2018/4/3 0003.
 */
@Api(description="RentSale接口",produces="application/json")
@Controller
@RequestMapping("rentForSale")
public class RentForSaleController {
    @Autowired
    private RentForSaleService rentForSaleService;

    /**
     * 出租出售图片返回url
     * @param file
     * @return
     */
    @ApiOperation(value = "出租出售处理图片接口", httpMethod = "POST", notes = "出租出售处理图片接口")
    @RequestMapping(value = "file",method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution rentOrSale(MultipartFile file){
        String url = UploadUtil.saveFile(file, FilePaths.RENTSALE);
        if(url == null || url.equals(null)){
            return new ReturnExcution(ReturnEnum.SAVE_FILE_FAIL,null);
        }
        url = "developer/"+url;
        return new ReturnExcution(ReturnEnum.FIND_SUCCESS,url);
    }


    /**
     * 出租出售
     * @param rentForSaleDto
     * @return
     */
    @ApiOperation(value = "出租出售接口", httpMethod = "POST", notes = "出租出售接口")
    @RequestMapping(value = "rentOrSale",method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution rentOrSale(RentForSaleDto rentForSaleDto){
        int save = rentForSaleService.rentOrSale(rentForSaleDto);
        if(save == 0){
            return new ReturnExcution(ReturnEnum.FIND_NOT_FAIL_CUSTOMER,null);
        }else if (save == 1){
            return new ReturnExcution(ReturnEnum.ADD_SUCCESS,save);
        }
        return new ReturnExcution(ReturnEnum.ADD_UPDATE_SUCCESS,save);
    }

    /**
     * 出租出售接口+传图片
     * @param rentForSaleDto
     * @return
     */
    @ApiOperation(value = "出租出售接口+传图片", httpMethod = "POST", notes = "出租出售接口+传图片")
    @RequestMapping(value = "rentOrSaleFile",method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution rentOrSaleFile(RentForSaleDto rentForSaleDto){
        int save = rentForSaleService.rentOrSaleFile(rentForSaleDto);
        if(save == 0){
            return new ReturnExcution(ReturnEnum.FIND_NOT_FAIL_CUSTOMER,null);
        }else if (save == 1){
            return new ReturnExcution(ReturnEnum.ADD_SUCCESS,save);
        }
        return new ReturnExcution(ReturnEnum.ADD_UPDATE_SUCCESS,save);
    }

    /**
     * 出租出售信息查询
     * @param
     * @return
     */
    @ApiOperation(value = "出租出售信息查询", httpMethod = "POST", notes = "出租出售信息查询")
    @RequestMapping(value = "findRentSaleData",method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution findRentSaleData(Integer houseId){
        Map map = rentForSaleService.findRentSaleData(houseId);
        if(map == null){
            return new ReturnExcution(ReturnEnum.FIND_HOUSE_SELL_NULL,null);
        }
        return new ReturnExcution(ReturnEnum.FIND_SUCCESS,map);
    }
}
